-module(mj_zzmj).
-include("common.hrl").

-export([get_all/0,start/0,check_start_hu/1,check_hu/2,test_actions_self/2,test_actions_other/1,get_score/3,check_ting/2]).

check_action(_MyData,{?ACTION_GAME_CHI,_,_}) -> false;
check_action(MyData,ActionData) ->
	mj_normal:check_action(MyData,ActionData).
  
get_all() ->
		[1011,1021,1031,1041,1051,1061,1071,1081,1091] ++
		[1012,1022,1032,1042,1052,1062,1072,1082,1092] ++
		[1013,1023,1033,1043,1053,1063,1073,1083,1093] ++
		[1014,1024,1034,1044,1054,1064,1074,1084,1094] ++
		[2011,2021,2031,2041,2051,2061,2071,2081,2091] ++
		[2012,2022,2032,2042,2052,2062,2072,2082,2092] ++
		[2013,2023,2033,2043,2053,2063,2073,2083,2093] ++
		[2014,2024,2034,2044,2054,2064,2074,2084,2094] ++
		[3011,3021,3031,3041,3051,3061,3071,3081,3091] ++
		[3012,3022,3032,3042,3052,3062,3072,3082,3092] ++
		[3013,3023,3033,3043,3053,3063,3073,3083,3093] ++
		[3014,3024,3034,3044,3054,3064,3074,3084,3094].
get_all_qp() ->
		[101,102,103,104,105,106,107,108,109] ++
		[201,202,203,204,205,206,207,208,209] ++
		[301,302,303,304,305,306,307,308,309].

rand_list([],Ret) -> Ret;
rand_list(L,Ret) ->
	Rand = util:rand(1, erlang:length(L)),
	Item = lists:nth(Rand, L),
	rand_list(lists:sublist(L, Rand  - 1) ++ lists:sublist(L, Rand  + 1,erlang:length(L) - Rand),Ret ++ [Item]).
	
start() ->
	L = get_all(),
	RL = rand_list(L,[]),
	{ok,?GAME_TYPE_ZZMAJIANG,lists:sort(lists:sublist(RL, 1, 14)),lists:sort(lists:sublist(RL, 15, 13)),
	 lists:sort(lists:sublist(RL, 28, 13)),lists:sort(lists:sublist(RL, 41, 13)),lists:sublist(RL, 54, 55)}.

check_start_hu(_L) -> false.

check_hu(L,Fields) ->
	SNL =  mj_normal:get_snl_by_qp(L),
	case mj_normal:check_normal_zm(SNL, Fields,false) of
		{true,NewFields,_} ->
			AGS = lists:filter(fun(#pt_public_field_data{sort = Sort}) -> Sort == ?FIELD_AGANG end, NewFields),
			MGS = lists:filter(fun(#pt_public_field_data{sort = Sort}) -> Sort == ?FIELD_MGANG end, NewFields),
			{true,erlang:length(AGS),erlang:length(MGS)};
		_ -> false
	end.
check_hu(L,Fields,Qp) ->
	SNL =  mj_normal:get_snl_by_qp(L ++ [Qp]),
	case mj_normal:check_normal_pao(SNL, Fields,Qp div 10,false) of
		{true,NewFields,_} ->
			AGS = lists:filter(fun(#pt_public_field_data{sort = Sort}) -> Sort == ?FIELD_AGANG end, NewFields),
			MGS = lists:filter(fun(#pt_public_field_data{sort = Sort}) -> Sort == ?FIELD_MGANG end, NewFields),
			DGS = lists:filter(fun(#pt_public_field_data{sort = Sort,data = Data}) -> Sort == ?FIELD_MGANG andalso Data == Qp div 10 end, NewFields),
			case DGS of
				[] -> {true,false,erlang:length(AGS),erlang:length(MGS)};
				_ -> {true,true,erlang:length(AGS),erlang:length(MGS)}
			end;
		_ -> false
	end.

test_actions_self(MyData = {_Index,Qps,Fields},CanHU) ->
	AGPS1 = mj_normal:get_agangs_peng(MyData),
	AGPS = [#pt_public_action_data{action = ?ACTION_GAME_GANG,action_data1 = Qpa,del_field = Field} || {Qpa,Field} <-AGPS1],
	AGPQ1 = mj_normal:get_agangs_qp(MyData),
	AGPQ = [#pt_public_action_data{action = ?ACTION_GAME_GANG,action_data1 = Qp1,action_data2 = Qp2,action_data3 = Qp3,action_data4 = Qp4} || {Qp1,Qp2,Qp3,Qp4} <-AGPQ1],
	HU = case CanHU of
			 true ->
				 case check_hu(Qps,Fields) of
					 {true,_,_} -> [#pt_public_action_data{action = ?ACTION_GAME_HU}];
					 _ -> []
				 end;
			 _ -> []
		 end,
	AGPS ++ AGPQ ++ HU ++ [#pt_public_action_data{action = ?ACTION_GAME_OUT}].

test_actions_other(MyData = {_Index,Qps,Fields}) ->
	#room_last{last_qp = LastQp} = get(room_last),
	?debug("LastQp = ~p",[LastQp]),
	Actions = [{?ACTION_GAME_PENG,LastQp},{?ACTION_GAME_GANG,LastQp}],
	?debug("Actions = ~p",[Actions]),
	HU = case check_hu(Qps ++ [LastQp],Fields,LastQp) of
			 {ok,_,_} -> [#pt_public_action_data{action = ?ACTION_GAME_HU,action_data1 = LastQp}];
			 _ -> []
		 end,
	test_actions(Actions,MyData,[]) ++ HU ++ [#pt_public_action_data{action = ?ACTION_GAME_GUO}].
	
test_actions([],_MyData,Ret)-> Ret;
test_actions([Action | NextActions],MyData,Ret)-> 
	?debug("Action = ~p,MyData = ~p",[Action,MyData]),
	case check_action(MyData, Action) of
		{ok,ActionData,_NextQp} ->
			?debug("ActionData = ~p",[ActionData]),
			test_actions(NextActions,MyData,Ret ++ [ActionData]);
		_R -> 
			?debug("_R = ~p",[_R]),
			test_actions(NextActions,MyData,Ret)
	end.

get_score(_HSort,_AferHSort,true) -> 20;
get_score(_HSort,_AferHSort,_) -> 10.

get_all_snl(L,Fields) ->
	SNL1 = lists:foldl(fun(Data,List) -> 
							   case lists:keyfind(Data div 10, 1, List) of
								   {_,OldNum} -> lists:keyreplace(Data div 10, 1, List, {Data div 10,OldNum + 1});
								   _ -> List ++ [{Data div 10,1}]
							   end
					   end, [], L),
	lists:foldl(fun(#pt_public_field_data{sort = Sort,data = Data},List) -> 
						case Sort of
							?FIELD_AGANG -> List ++ [{Data,4}];
							?FIELD_MGANG -> List ++ [{Data,4}];
							?FIELD_PENG -> List ++ [{Data,3}];
							_ -> List
						end
				end, SNL1, Fields).

check_ting(L,Fields) ->
	Qps = get_all_qp(),
	Pred = fun(Qp) ->
				   SNL = get_all_snl(L,Fields),
				   case lists:keyfind(Qp, 1, SNL) of
					   [{_,4}] -> false;
					   _ ->
						   case check_hu(L,Fields,Qp * 10 + 1) of
							   {true,_,_,_} -> true;
							   _ -> false
						   end
				   end
		   end,  
	lists:any(Pred, Qps).
	
